package com.giantlizard.cloud.mybatis.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.giantlizard.cloud.base.entity.BaseEntity;
import com.giantlizard.cloud.base.entity.PageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * IBaseService 实现类（ 泛型：M 是 mapper 对象，T 是实体 ）
 * @author kangdongwei
 * @since 2024-07-23
 * @version 1.0
 */
@Slf4j
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity, D>  extends ServiceImpl<M, T> implements IBaseService<T, D> {
    @Override
    public D selectById(String id) throws Exception {
        T entity = super.getById(id);
        return transForToDto(entity);
    }

    @Override
    public String insert(D  dto) throws Exception {
        T  entity = transForToEntity(dto);
        super.save(entity);
        return entity.getId();
    }

    @Override
    public boolean update(D dto) throws Exception {
        T  entity = transForToEntity(dto);
        return  super.updateById(entity);
    }

    @Override
    public boolean updateSelective(D dto) throws Exception {
        T  entity = transForToEntity(dto);
        return  super.updateById(entity);
    }

    @Override
    public boolean deleteById(String id) throws Exception {
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteList(List<String> ids) throws Exception {
        for (String id : ids) {
            if (!super.removeById(id)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public List<D> query(D dto) throws Exception {
        QueryWrapper<T> queryWrapper = createQueryWrapper(dto,null);
        List<T>  queryEntityList = super.list(queryWrapper);
        return transForToDto(queryEntityList);
    }

    @Override
    public IPage<D> queryByPage(D dto, PageQuery page) throws Exception {
        QueryWrapper<T> queryWrapper = createQueryWrapper(dto,page);
        IPage<T> pageResult =  super.page(new Page<>(page.getPageNum(), page.getPageSize()), queryWrapper);
        return transForToDtoPage(pageResult);
    }

    @Override
    public List<D> transForToDto(List<T> entityList) throws Exception {
        List<D> dtoList = new ArrayList<>();
        entityList.forEach( o -> {
            try {
                dtoList.add(transForToDto(o));
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        });
        return dtoList;
    }

    @Override
    public IPage<D> transForToDtoPage(IPage<T> page) throws Exception {
        IPage<D> dtoPage = new Page<>();
        List<D> dtoList = new ArrayList<>();
        page.getRecords().forEach( o -> {
            try {
                dtoList.add(transForToDto(o));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        BeanUtils.copyProperties(page, dtoPage);
        dtoPage.setRecords(dtoList);
        return dtoPage;
    }

}
